package dou;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class xian {
    static List<String> list = new ArrayList<String>();
    static CountDownLatch latch = new CountDownLatch(1);

    public static void work() {
        String tn = Thread.currentThread().getName();
        System.out.println(tn + "线程启动");
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add("item" + (list.size() + 1));
            System.out.println(String.format("%s:%d=%s", tn, list.size(), list.get(list.size() - 1)));
            if (list.size() == 5) {
                latch.countDown();
            }
        }
    }

    public static void check() {
        String tn = Thread.currentThread().getName();
        System.out.println(tn + ":线程启动");
        try {
            latch.await();
            System.out.println(tn + ":容器中已经有五个元素了，线程结束！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(xian::work, "T1").start();
        new Thread(xian::check, "T2").start();
    }
}